package cn.ll.po;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Connector entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.ll.po.Connector
 * @author MyEclipse Persistence Tools
 */

public class ConnectorDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(ConnectorDAO.class);
	// property constants
	public static final String CONNECTORID = "connectorid";
	public static final String CONNECTORNAME = "connectorname";
	public static final String POSSITION = "possition";
	public static final String EMAIL = "email";
	public static final String TEL = "tel";
	public static final String MB = "mb";
	public static final String AGE = "age";
	public static final String SEX = "sex";
	public static final String HOBE = "hobe";
	public static final String NOTES = "notes";
	public static final String CUSTOMERID = "customerid";

	protected void initDao() {
		// do nothing
	}

	public void save(Connector transientInstance) {
		log.debug("saving Connector instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Connector persistentInstance) {
		log.debug("deleting Connector instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Connector findById(String id) {
		log.debug("getting Connector instance with id: " + id);
		try {
			Connector instance = (Connector) getHibernateTemplate().get("cn.ll.po.Connector", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Connector> findByExample(Connector instance) {
		log.debug("finding Connector instance by example");
		try {
			List<Connector> results = (List<Connector>) getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Connector instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Connector as model where model." + propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Connector> findByConnectorid(Object connectorid) {
		return findByProperty(CONNECTORID, connectorid);
	}

	public List<Connector> findByConnectorname(Object connectorname) {
		return findByProperty(CONNECTORNAME, connectorname);
	}

	public List<Connector> findByPossition(Object possition) {
		return findByProperty(POSSITION, possition);
	}

	public List<Connector> findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	public List<Connector> findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List<Connector> findByMb(Object mb) {
		return findByProperty(MB, mb);
	}

	public List<Connector> findByAge(Object age) {
		return findByProperty(AGE, age);
	}

	public List<Connector> findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List<Connector> findByHobe(Object hobe) {
		return findByProperty(HOBE, hobe);
	}

	public List<Connector> findByNotes(Object notes) {
		return findByProperty(NOTES, notes);
	}

	public List<Connector> findByCustomerid(Object customerid) {
		return findByProperty(CUSTOMERID, customerid);
	}

	public List findAll() {
		log.debug("finding all Connector instances");
		try {
			String queryString = "from Connector";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Connector merge(Connector detachedInstance) {
		log.debug("merging Connector instance");
		try {
			Connector result = (Connector) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Connector instance) {
		log.debug("attaching dirty Connector instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Connector instance) {
		log.debug("attaching clean Connector instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ConnectorDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ConnectorDAO) ctx.getBean("ConnectorDAO");
	}

	public Long countByCustomerId(String customerid) {

		log.debug("finding all Connector instances");
		try {
			String queryString = "select count(*) from Connector model where model.customerid = ?";

			List ls = getHibernateTemplate().find(queryString, new Object[] { customerid });
			return (Long) ls.get(0);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}